package org.jpro.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.jpro.common.util.JsonMapper;
import org.jpro.servlet.KaptchaServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import lombok.extern.slf4j.Slf4j;

/**
 * <P>功能描述 : mvc配置 </P>
 * <P>File name : WebConfig </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017/3/4 上午2:14</P>
 */
@Slf4j
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        // 定义无Controller的path<->view直接映射
        registry.addViewController("/").setViewName("/system/index");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 自定义资源映射 @see https://my.oschina.net/liuyuantao/blog/807125
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * <P>功能描述 : 将Jackson2HttpMessageConverter的默认格式化输出为false </P>
     * <P>Method Name : mappingJacksonHttpMessageConverter </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-05 11:13:24</P>
     * @return the mapping jackson 2 http message converter
     * @since 1.0
     */
    @Bean
    @Primary
    public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        converter.setSupportedMediaTypes(supportedMediaTypes);
        converter.setObjectMapper(new JsonMapper());
        return converter;
    }

    /**
     * <P>功能描述 : REST中根据URL后缀自动判定Content-Type及相应的View </P>
     * <P>Method Name : contentNegotiationManagerFactoryBean </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-05 11:12:52</P>
     * @return the content negotiation manager factory bean
     * @since 1.0
     */
    @Bean
    @Primary
    public ContentNegotiationManagerFactoryBean contentNegotiationManagerFactoryBean() {
        ContentNegotiationManagerFactoryBean contentNegotiationManagerFactoryBean = new ContentNegotiationManagerFactoryBean();
        contentNegotiationManagerFactoryBean.setFavorParameter(true);
        contentNegotiationManagerFactoryBean.setIgnoreAcceptHeader(true);
        contentNegotiationManagerFactoryBean.setDefaultContentType(MediaType.TEXT_HTML);
        contentNegotiationManagerFactoryBean.addMediaType("xml", MediaType.APPLICATION_XML);
        contentNegotiationManagerFactoryBean.addMediaType("json", MediaType.APPLICATION_JSON);
        return contentNegotiationManagerFactoryBean;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 将StringHttpMessageConverter的默认编码设为UTF-8
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
    }

    /**
     * <P>功能描述 : 上传文件拦截，设置最大上传文件大小 10M=10*1024*1024(B)=10485760 bytes</P>
     * <P>Method Name : commonsMultipartResolver </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-05 11:17:14</P>
     * @return the commons multipart resolver
     * @since 1.0
     */
    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver commonsMultipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        // TODO: 2017/3/5 以后写到配置里面
        commonsMultipartResolver.setMaxInMemorySize(10485760);
        return commonsMultipartResolver;
    }

    /**
     * <P>功能描述 : kaptcha验证码 </P>
     * <P>Method Name : kaptchaServlet </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-05 11:28:49</P>
     * @return the servlet registration bean
     * @since 1.0
     */
    @Bean
    public ServletRegistrationBean kaptchaServlet() {

        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();

        servletRegistrationBean.setServlet(new KaptchaServlet());

        // 映射地址
        List<String> list = new ArrayList<>();
        list.add("/static/images/kaptcha.jpg");
        servletRegistrationBean.setUrlMappings(list);

        return servletRegistrationBean;

    }

}